package com.cedifie.lendapp.utils.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cedifie.lendapp.R
import com.cedifie.lendapp.databinding.DialogPermissionReasonBinding
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.utils.event.CedEventType
import com.cedifie.lib_baseadapter.CEDRecyclerViewAdapter
import com.cedifie.lib_baseadapter.CEDViewHolderHelper
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/14 15:50
 * @Annotation：权限说明dialog
 */
class ReasonDialog: ProDialog {
    private lateinit var binding: DialogPermissionReasonBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    //权限描述列表
    private var permissionAdapter: PItemAdapter? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogPermissionReasonBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        val layoutManager = LinearLayoutManager(context)
        binding.recyclerView.layoutManager = layoutManager
        permissionAdapter = PItemAdapter(binding.recyclerView)
        permissionAdapter?.setOnItemChildClickListener { _, _, position ->
            val item = permissionAdapter?.getItem(position)
        }
        binding.recyclerView.adapter = permissionAdapter
    }

    override fun initListener() {
        setOnClickListener(R.id.btnAgree)
        setOnClickListener(R.id.btnDeny)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btnDeny -> {
                mCallback?.onReasonDenyClick()
            }
            R.id.btnAgree -> {
                mCallback?.onReasonAgreeClick()
            }
        }
    }

    interface IDialogCallback {
        /**
         * 同意
         */
        fun onReasonAgreeClick()
        /**
         * 拒绝
         */
        fun onReasonDenyClick()
    }

    /**
     * 展示权限说明
     */
    fun showCedPermissionItemViews(data: JSONObject){
        binding.imageIcon.setImageResource(data.optInt("icon"))
        binding.textPermissionTitle.text = data.optString("title")

        val dataList = arrayListOf<JSONObject>()
        val array = data.optJSONArray("msgList")
        if (array != null){
            for (i in 0..< array.length()){

                dataList.add(array.optJSONObject(i))
            }
        }
        permissionAdapter?.data = dataList
    }

    class PItemAdapter(recyclerView: RecyclerView?) : CEDRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_permission_reason_child) {
        override fun fillData(helper: CEDViewHolderHelper, position: Int, model: JSONObject) {
            val showPermissionTitle = model.optString("title")
            if (showPermissionTitle.isEmpty()){
                helper.setVisibility(R.id.text_title,View.GONE)
            } else {
                helper.setVisibility(R.id.text_title,View.VISIBLE)
                helper.setText(R.id.text_title,showPermissionTitle)
            }
            helper.setText(R.id.text_reason,model.optString("des"))
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addCedEventAction(CedEventType.DIALOG.CED_DIALOG_PERMISSION_REASON_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addCedEventAction(CedEventType.DIALOG.CED_DIALOG_PERMISSION_REASON_DISMISS,eventRemark)
    }
}